package zozopo.manual;

import java.io.IOException;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.interceptor.ServletRequestAware;
import org.apache.struts2.interceptor.ServletResponseAware;
import org.apache.struts2.interceptor.SessionAware;

import zozopo.comm.Validate;
import zozopo.database.BusiDatabaseManager;
import zozopo.manual.operations.ReleaseOS;
import zozopo.manual.operations.ReleaseTB;
import zozopo.manual.operations.ReplyOS;
import zozopo.manual.operations.ReplyTB;
import zozopo.operations.business.IPReportOS;
import zozopo.operations.business.IPReportTB;
import zozopo.operations.business.ad.GoodsOS;
import zozopo.operations.business.ad.GoodsTB;
import zozopo.operations.comm.DictionaryOS;

import com.opensymphony.xwork2.Action;
import com.opensymphony.xwork2.ActionSupport;

public class ViewAction extends ActionSupport implements Action ,ServletRequestAware,ServletResponseAware{
	
	private String subject;
	private String lable;
	private String messageTemp;
	private String brand;
	private String date;
	private String userMail;
	private String ip;
	private String id;
	private String from;
	
	//传回参数
	private List<ReplyTB> replyList;
	private ReleaseTB release;
	private List<ReleaseTB> relatedBrandList;
	private List<ReleaseTB> storeList;
	private List<ReleaseTB> storeKindList;
	private List<GoodsTB> goodsList; 
	
	private int countPage = 0;
	private int beginPageNum = 0;
	private int pageSize = 10;
	private int currentPage;
	private int rowNum = 0;
	
	public String getFrom() {
		return from;
	}

	public void setFrom(String from) {
		this.from = from;
	}
	
	public int getCountPage() {
		return countPage;
	}

	public void setCountPage(int countPage) {
		this.countPage = countPage;
	}

	public int getBeginPageNum() {
		return beginPageNum;
	}

	public void setBeginPageNum(int beginPageNum) {
		this.beginPageNum = beginPageNum;
	}

	public int getPageSize() {
		return pageSize;
	}

	public void setPageSize(int pageSize) {
		this.pageSize = pageSize;
	}

	public int getCurrentPage() {
		return currentPage;
	}

	public void setCurrentPage(int currentPage) {
		this.currentPage = currentPage;
	}

	public int getRowNum() {
		return rowNum;
	}

	public void setRowNum(int rowNum) {
		this.rowNum = rowNum;
	}

	public ReleaseTB getRelease() {
		return release;
	}

	public void setRelease(ReleaseTB release) {
		this.release = release;
	}

	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

	public List<ReleaseTB> getRelatedBrandList() {
		return relatedBrandList;
	}

	public void setRelatedBrandList(List<ReleaseTB> relatedBrandList) {
		this.relatedBrandList = relatedBrandList;
	}

	public String getIp() {
		return ip;
	}

	public void setIp(String ip) {
		this.ip = ip;
	}

	public String getUserMail() {
		return userMail;
	}

	public void setUserMail(String userMail) {
		this.userMail = userMail;
	}

	public String getDate() {
		return date;
	}

	public void setDate(String date) {
		this.date = date;
	}

	HttpServletRequest request;
	HttpServletResponse response;
	
	public String execute(){
		if(id == null || !Validate.isNumber(id)){
			return ERROR;
		}
		
		
		int subjectId = Integer.parseInt(id);
		id = ""+subjectId;
		//int subjectId = id;
		BusiDatabaseManager databaseConnection = new BusiDatabaseManager();
		Connection conn = databaseConnection.getConnection();
		
		
	//	if()
		
		ReleaseOS releaseOS = new ReleaseOS(conn);
		
		release = releaseOS.getSubjectById(subjectId);
		
		if(release == null){
			databaseConnection.releaseConnection(conn);
			return ERROR;
		}
		release.setRelease_context(Validate.htmlLabelFilter(release.getRelease_context()));
			
		//release.setRelease_brand(release.getRelease_brand()); // encoder for view.
		
		
		String releaseFrom = release.getRelease_from();
		
		if(releaseFrom != null && releaseFrom.equals("LOCAL")){
			releaseOS.views(subjectId);
			try {
				response.sendRedirect(request.getContextPath()+release.getRelease_url());
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			//对本地连接进行特殊URL跳转
		}else if(from != null && from.equals("LOCAL")){
			releaseOS.views(subjectId);
			try {
				response.sendRedirect(request.getContextPath()+release.getRelease_url());
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}else if(from != null && from.equals("REMOTE")){   //对外来连接进行跳转
			releaseOS.views(subjectId);
			
			IPReportOS ipReportOS = new IPReportOS(conn);
			IPReportTB ipreport = new IPReportTB();
			
			ipreport.setName_ipreport(release.getRelease_subject());
			ipreport.setUrl_ipreport(release.getRelease_url());
			ipreport.setIp_ipreport(getIpAddr(request));
			
			ipReportOS.insertReport(ipreport);
			
			try {
				response.sendRedirect(release.getRelease_url());
				databaseConnection.releaseConnection(conn);
				return NONE;
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}else{
			ReplyOS replyOS = new ReplyOS(conn);
			GoodsOS goodsOS = new GoodsOS(conn);
			//releaseOS.setConn(conn);
			rowNum = replyOS.getReplyRowNumBySubjectId(subjectId);
			//countPage = rowNum/pageSize+1;
			replyList = replyOS.selectReplyBySubjectId(subjectId, beginPageNum, pageSize);
			
			DictionaryOS dictionaryOS = new DictionaryOS(conn);
			//dictionaryOS.setConn(conn);
			releaseOS.views(subjectId);
			storeList = releaseOS.selectNewByBrandAndKind(release.getRelease_brand(), "KIND.RECOMMENDSTORE", 0, 10);
			storeKindList = releaseOS.selectHitByKindAndLable("KIND.RECOMMENDSTORE", release.getRelease_lable(), 0, 5);
			goodsList = goodsOS.selectGoodsByCnbrandAndEnbrand(release.getRelease_brand(), release.getRelease_secondbrand(), 1, 3);
			
			setBrand(release.getRelease_brand());
			setLable(release.getRelease_lable());
			setMessageTemp(release.getRelease_context());
			setSubject(release.getRelease_subject());
			setDate(release.getRelease_date());
			setUserMail(release.getRelease_useremail());
			setIp(release.getRelease_ip());
			
			relatedBrandList = releaseOS.selectRelatedBrand(brand, 0, 20);
			lable = dictionaryOS.getDictValue(lable);
			//messageTemp = Validate.htmlLabelFilter(messageTemp);
			//request.setAttribute("message", messageTemp);
		}
			
		//request.setAttribute("relatedBrandSet", );
		
		databaseConnection.releaseConnection(conn);
		
		return SUCCESS;
	}

	public List<ReleaseTB> getStoreList() {
		return storeList;
	}

	public void setStoreList(List<ReleaseTB> storeList) {
		this.storeList = storeList;
	}

	public List<ReplyTB> getReplyList() {
		return replyList;
	}

	public void setReplyList(List<ReplyTB> replyList) {
		this.replyList = replyList;
	}

	public String getSubject() {
		return subject;
	}

	public void setSubject(String subject) {
		this.subject = subject;
	}

	public String getLable() {
		return lable;
	}

	public void setLable(String lable) {
		this.lable = lable;
	}

	public String getMessageTemp() {
		return messageTemp;
	}

	public void setMessageTemp(String messageTemp) {
		this.messageTemp = messageTemp;
	}

	public String getBrand() {
		return brand;
	}

	public void setBrand(String brand) {
		this.brand = brand;
	}

	public void setServletRequest(HttpServletRequest arg0) {
		// TODO Auto-generated method stub
		request = arg0;
		
	}
	
	public void setServletResponse(HttpServletResponse arg0) {
		// TODO Auto-generated method stub
		response = arg0;
		
	}

	public List<ReleaseTB> getStoreKindList() {
		return storeKindList;
	}

	public void setStoreKindList(List<ReleaseTB> storeKindList) {
		this.storeKindList = storeKindList;
	}
	
	/**
     * 获得客户端真是ip
     */
    public static String getIpAddr(HttpServletRequest request) {
        String ip = request.getHeader("x-forwarded-for");    
        if(ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {    
            ip = request.getHeader("Proxy-Client-IP");    
        }    
        if(ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {    
            ip = request.getHeader("WL-Proxy-Client-IP");    
        }    
        if(ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {    
            ip = request.getRemoteAddr();    
     }
		return ip; 
    }

	public List<GoodsTB> getGoodsList() {
		return goodsList;
	}

	public void setGoodsList(List<GoodsTB> goodsList) {
		this.goodsList = goodsList;
	}
}
